package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import com.itheima.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import java.util.UUID;

/**
 * (服务消费方)体检套餐管理
 */
@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Reference
    private SetmealService setmealService;
    @Autowired//操作redis的连接池(来自于spring-redis.xml里的jedisPool)
    private JedisPool jedisPool;

    //文件上传，使用springMVC还需要在resources里的springmvc.xml配置一个文件上传的组件：
//        <!--文件上传解析器组件-->
//    <bean id="multipartResolver"
//    class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
//        <property name="maxUploadSize" value="104857600" />
//        <property name="maxInMemorySize" value="4096" />
//        <property name="defaultEncoding" value="UTF-8"/>
//    </bean>

    //图片上传
    @RequestMapping("/upload")//指定上传的值<el-upload name="imgFile">
    public Result upload(@RequestParam("imgFile") MultipartFile imgFile){
        //原始文件名
        String originalFilename = imgFile.getOriginalFilename();
        //获取原文件名中最后一个.的索引位置
        int index = originalFilename.lastIndexOf(".");
        //获取原文件后缀
        String suffix = originalFilename.substring(index - 1);// - 1表示包含.
        //获得.jpg
        String fileName = UUID.randomUUID().toString() + suffix;
        //将图片保存到七牛云
        try{
            QiniuUtils.upload2Qiniu(imgFile.getBytes(),fileName);
            //文件上传成功后，需要将文件名称保存到redis中(这个是所有上传到七牛云的图片名)
            // (在七牛云一定有数据，但是在数据库不一定有记录的图片是垃圾图片)
            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES,fileName);//sadd操作的是set集合
            //返回前端文件名称，http://smx5aluol.hn-bkt.clouddn.com/"+response.data;用于回显示
            return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS,fileName);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.PIC_UPLOAD_FAIL);
        }
    }

    //新增体检套餐
    @RequestMapping("/add")
    public Result add(@RequestBody Setmeal setmeal,Integer[] checkgroupIds){
        try{
            setmealService.add(setmeal,checkgroupIds);
            System.out.println("setmealService.add调用成功");
            return new Result(true,MessageConstant.ADD_SETMEAL_SUCCESS);
        }catch (Exception e){
            System.out.println("setmealService.add调用失败");
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_SETMEAL_FAIL);
        }
    }

    //分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        return setmealService.findPage(queryPageBean);
    }
}
